/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.shape.annotation.impl;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import org.apache.log4j.Logger;

import who.tmap.toolkit.shape.annotation.TextShape;
import who.tmap.util.MiscHelper;

/**
 * A class dedicated to text annotations
 * @author stl
 */
public class TextShapeImpl extends BaseTextMapShapeImpl implements TextShape {

    private static Logger logger = Logger.getLogger(TextShapeImpl.class);  
    
	private String text;
	protected Font font;
	protected double textShapeFactor;

	public TextShapeImpl() {
		super();
		setType( TEXT );
	}
	
	/**
	 * @return
	 */
	public String getText() {
		return text;
	}

	/**
	 * @param string
	 */
	public void setText(String string) {
		text = string;
	}
	
	protected Font getScaledFont( Graphics g ) {
		font = new Font( fontName, fontStyle, fontSize );
				
		// Special case for TextShape where map factor has to be overriden
		// because we want font size to be independent from map factor 
		AffineTransform at =new AffineTransform();
		textShapeFactor =getScreenToMapFactor() * getZoomFactor() / 100.d;
		at.scale( textShapeFactor, textShapeFactor );
		
		Font scaledFont =font.deriveFont( at );
		
		return scaledFont;
	}
	
	protected TextLayout getTextLayout( Graphics g, String text ) {
		FontRenderContext frc = ((Graphics2D)g).getFontRenderContext();
		
		TextLayout textLayout = new TextLayout(text, getScaledFont( g ), frc);

		return textLayout;
	}
	
	protected Rectangle2D drawText( Graphics g, String text, Point2D pt2d ) { 
		TextLayout textLayout =getTextLayout( g, text );
		
		textLayout.draw( ((Graphics2D)g), (float)pt2d.getX(), (float)pt2d.getY() + textLayout.getAscent() );		
		
		Rectangle2D textBounds =textLayout.getBounds();
		textBounds.setRect( textBounds.getX() + pt2d.getX(),
							textBounds.getY() + pt2d.getY() + textLayout.getAscent(),
							textBounds.getWidth(),
							textBounds.getHeight() );	
		
		return textBounds;
	}	
	
	protected Rectangle2D drawTextForMacOS( Graphics g, String text, Point2D pt ) {
		Graphics2D g2 =(Graphics2D)g;
		Font savFont =g2.getFont();

		Font scaledFont =getScaledFont( g );
		g2.setFont( scaledFont );

		GlyphVector gv = scaledFont.createGlyphVector(g2.getFontRenderContext(), text);
		Rectangle2D bounds =font.getStringBounds(text, g2.getFontRenderContext());
		bounds.setRect( pt.getX() + (bounds.getX()*textShapeFactor) 
			  		  , pt.getY() + (bounds.getY()*textShapeFactor) 
			  		  , bounds.getWidth()*textShapeFactor 
			  		  , bounds.getHeight()*textShapeFactor );

		gv.performDefaultLayout();
		g2.drawGlyphVector(gv, (float)pt.getX(), (float)pt.getY() );
		
		g2.setFont( savFont );
		  
		return bounds;
	}
	
	public void draw(Graphics g) {
		bounds =MiscHelper.isMacOS() ?  drawTextForMacOS( g, text, pt )
									 :	drawText( g, text, pt );

		displaySelectionHandles( g );
	}
    
}
